#!/bin/bash -xe

kata_coco_top_dir="$HOME/workspace/CoCo"

guest_components_repo_url="https://gitee.com/hanliyang-kata-coco/guest-components.git"
guest_components_repo_branch="0.10.0-hygon"
guest_components_repo_dir=${kata_coco_top_dir}/guest-components

if [ ! -e $guest_components_repo_dir ]; then
  git clone ${guest_components_repo_url} \
    -b ${guest_components_repo_branch} \
    ${guest_components_repo_dir}
fi

coco_guest_components_version="v0.10.0"
coco_guest_components_tag="$coco_guest_components_version"

build_coco_keyprovider_docker_image () {
  echo "Build CoCo Key Provider docker image"
  pushd ${guest_components_repo_dir}

  sudo docker build \
    --build-arg HTTPS_PROXY=${https_proxy} \
    --build-arg HTTP_PROXY=${http_proxy} \
    -t coco-keyprovider:${coco_guest_components_tag} \
    -f ./attestation-agent/docker/Dockerfile.keyprovider \
    .

  popd # ${guest_components_repo_dir}

  docker image save \
    -o ${kata_coco_top_dir}/coco-keyprovider_${coco_guest_components_tag}.tar \
    coco-keyprovider:${coco_guest_components_tag}
}

build_coco_keyprovider_docker_image
